package com.foton.bpc.all.remoting.netty.handler;

import com.foton.bpc.all.remoting.dto.BpcInvacation;
import com.foton.bpc.all.remoting.dto.Response;
import com.foton.bpc.all.remoting.invoker.Invoker;
import com.foton.bpc.all.remoting.netty.adpect.HandlerAdpect;
import com.foton.bpc.all.remoting.netty.channel.BpcChannel;
import com.foton.bpc.all.serialzation.Serialzation;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author RenPu
 * @Date 2023/6/6 17:49
 * @Version 1.0
 * @Description: 服务端得处理器（服务提供者）
 **/

@Slf4j
public class BpcServerHandler implements HandlerAdpect {
    /**
     * 代理对象
     */
    private Invoker invoker;
    private Serialzation serialzation;
    @Override
    public void onRecevice(BpcChannel bpcChannel, Object msg) throws Exception {
        BpcInvacation bpcInvacation =(BpcInvacation)msg;
        log.info("收到 ：：：： BpcInvacation信息"+bpcInvacation);

        //封装成response对象响应出去
        Response response = new Response();
        //todo 代理类进行方法调用
        try {
            Object invoker = getInvoker().invoker(bpcInvacation);   //发起代理类进行真正的方法调用
            response.setStatus(200);
            response.setRequestId(bpcInvacation.getId());
            response.setContent(invoker);
        } catch (Exception e) {
            response.setStatus(99);
            response.setContent(e.getMessage());
            e.printStackTrace();
        }

        byte[] serialzation = getSerialzation().serialzation(response);
        bpcChannel.send(serialzation);


    }

    @Override
    public void onWrite(BpcChannel bpcChannel, Object msg) {



    }


    public Invoker getInvoker() {
        return invoker;
    }

    public void setInvoker(Invoker invoker) {
        this.invoker = invoker;
    }


    public Serialzation getSerialzation() {
        return serialzation;
    }

    public void setSerialzation(Serialzation serialzation) {
        this.serialzation = serialzation;
    }
}
